package cn.tm.ms.nerver.strategy;

import cn.tm.ms.nerver.neural.Neural;
import cn.tm.ms.nerver.neural.entry.FaultTolerance;
import cn.tm.ms.nerver.neural.entry.NeuralConf;
import cn.tm.ms.nerver.neural.support.NeuralSupport;
import cn.tm.ms.nerver.strategy.entry.StrategyDo;

import com.netflix.hystrix.HystrixCommand.Setter;

/**
 * 容错核心拦截执行器
 * 
 * @author lry
 *
 * @param <REQ>
 * @param <RES>
 */
public class StrategyHandler<REQ, RES> {
	
	public RES execute(StrategyDo<REQ, RES> ftDo){
		if(ftDo.getFt()==null){
			ftDo.setFt(new FaultTolerance());
		}
		if(ftDo.getConf()==null){
			ftDo.setConf(new NeuralConf());
		}
		if(ftDo.getRoute()==null){
			throw new RuntimeException("The 'route' cannot be empty.");
		}
		
		//构建Setter
		Setter setter=NeuralSupport.buildByFaultTolerance(ftDo.getFt());
		//创建容错模块
		Neural<REQ, RES> ftModuler=new Neural<REQ, RES>(setter, ftDo.getConf(), ftDo.getRoute(), ftDo.getReq());
		//同步调用
		RES res = ftModuler.execute();
		
		return res;
	}

}
